/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useEffect, useState } from 'react';
import { Form, message, Radio, Card, List } from 'antd';
import KbPriority from '../KbPriority';
import {
  enableLeagueServiceFee,
  allowLogin,
  setAllowDeliveryStatus,
} from '@/services/jdk/register';
import styles from './index.less';

const { Item } = Form;
interface KbDrawer {
  league_id: string;
  service_fee_enable: string;
  service_fee_expire_type: string;
  allow_set_delivery_status: string;
  allow_login: string;
  callBack: Function;
}
const Index = ({
  league_id,
  service_fee_enable,
  service_fee_expire_type,
  allow_login,
  allow_set_delivery_status,
  callBack,
}: KbDrawer) => {
  const [formAuth] = Form.useForm();
  const [allow, setAllow] = useState(false);

  const handleSwitch = async (values: any) => {
    const { target: { value = '', name = '' } = {} } = values;
    if (name === 'service_fee_enable') {
      setAllow(value === '0');
    }
    const params = {
      [name]: value,
    };
    const apis = {
      allow_login: allowLogin,
      service_fee_enable: enableLeagueServiceFee,
      service_fee_expire_type: enableLeagueServiceFee,
      allow_set_delivery_status: setAllowDeliveryStatus,
    };
    // const apis = name === 'allow_login' ? allowLogin : enableLeagueServiceFee;
    const { code, msg } = await apis[name]({ league_id, ...params });
    if (code == 0) {
      callBack();
      message.success(msg);
    } else {
      message.error(msg);
    }
  };

  useEffect(() => {
    setAllow(service_fee_enable === '0');
    formAuth.setFieldsValue({
      league_id,
      service_fee_enable,
      service_fee_expire_type,
      allow_login,
      allow_set_delivery_status,
    });
  }, [service_fee_enable, service_fee_expire_type, allow_login, allow_set_delivery_status]);

  return (
    <Card title='权限设置' className={styles.drawerAuth}>
      <Form form={formAuth}>
        <List bordered>
          <List.Item>
            <div>是否允许服务商对下属商家设置基础价</div>
            <div style={{ textAlign: 'right' }}>
              <Item name='service_fee_enable'>
                <Radio.Group name='service_fee_enable' onChange={handleSwitch}>
                  <Radio value={'1'}>允许</Radio>
                  <Radio value={'0'}>不允许</Radio>
                </Radio.Group>
              </Item>
              {!allow ? (
                <Item label='设置有效期' name='service_fee_expire_type'>
                  <Radio.Group name='service_fee_expire_type' onChange={handleSwitch}>
                    <Radio value={'1m'}>1个月</Radio>
                    <Radio value={'3m'}>3个月</Radio>
                    <Radio value={'6m'}>6个月</Radio>
                    <Radio value={'undated'}>无限期</Radio>
                  </Radio.Group>
                </Item>
              ) : null}
            </div>
          </List.Item>
          <List.Item>
            <div>是否允许服务商一键登录其下属商家网页版聚单客账号</div>
            <div style={{ textAlign: 'right' }}>
              <Item name='allow_login'>
                <Radio.Group name='allow_login' onChange={handleSwitch}>
                  <Radio value={'1'}>允许</Radio>
                  <Radio value={'0'}>不允许</Radio>
                </Radio.Group>
              </Item>
            </div>
          </List.Item>
          <List.Item>
            <KbPriority league_id={league_id} />
          </List.Item>
          <List.Item>
            <div>是否允许服务商对下属上架启用/关闭发单权限</div>
            <div style={{ textAlign: 'right' }}>
              <Item name='allow_set_delivery_status'>
                <Radio.Group name='allow_set_delivery_status' onChange={handleSwitch}>
                  <Radio value={'1'}>允许</Radio>
                  <Radio value={'0'}>不允许</Radio>
                </Radio.Group>
              </Item>
            </div>
          </List.Item>
        </List>
      </Form>
    </Card>
  );
};

export default Index;
